package com.clp.protocol.iec104.iec104.client;

import com.clp.protocol.iec104.iec104.client.config.MasterInfoConfig;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * 连接持有者
 */
@Slf4j
public abstract class InMasterInfo implements MasterInfo {
    private final String remoteHost;
    private final int remotePort;
    private final int rtuAddr; // 公共地址验证

    protected InMasterInfo(MasterInfoConfig cfg) {
        // 设置通道、ip、端口号
        this.remoteHost = cfg.getRemoteHost();
        this.remotePort = cfg.getRemotePort();
        // 公共地址
        this.rtuAddr = cfg.getRtuAddr();
    }

    @Override
    public String remoteHost() {
        return remoteHost;
    }

    @Override
    public int remotePort() {
        return remotePort;
    }

    @Override
    public int rtuAddr() {
        return rtuAddr;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        InMasterInfo that = (InMasterInfo) o;
        return remotePort == that.remotePort && Objects.equals(remoteHost, that.remoteHost);
    }

    @Override
    public int hashCode() {
        return Objects.hash(remoteHost, remotePort);
    }

    @Override
    public String toString() {
        return "MasterInfo{" +
                "remoteHost='" + remoteHost() + '\'' +
                ", remotePort=" + remotePort() +
                ", rtuAddr=" + rtuAddr() +
                '}';
    }
}
